/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.grid;

import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Base action for all grid actions.
 * Contains some base methods for grid actions.
 * </p>
 * <p><a href="BaseGridAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.5 $ $Date: 2006/08/03 10:07:55 $
 */
public class BaseGridAction extends BaseAction {

	/**
	 * Gets grid by specified name from the session
	 *
	 * @param gridName Name of grid to search by
	 * @param session  Http session to search in
	 * @return Grid if it was found and else otherwise
	 */
	protected Grid getGridByName(String gridName, HttpSession session) {
		Grid grid = null;
		Map grids = (Map) session.getAttribute(TaglibConstants.GRIDS);
		if ( grids != null ) {
			grid = (Grid) grids.get(gridName);
		}
		return grid;
	}

	/**
	 * Saves grid in session
	 *
	 * @param grid    Grid to save
	 * @param session Http session to save in
	 */
	protected void saveGrid(Grid grid, HttpSession session) {
		Map grids = (Map) session.getAttribute(TaglibConstants.GRIDS);
		if ( grids == null ) {
			grids = Collections.synchronizedMap(new HashMap());
		}
		grids.put(grid.getName(), grid);
		session.setAttribute(TaglibConstants.GRIDS, grids);
	}

}